package com.morningstar.common.advice;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ResultCode;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author yanglianshan
 */
@Slf4j
@ControllerAdvice
public class SaTokenExceptionAdvice extends BaseExceptionAdvice {

    @ResponseBody
    @ExceptionHandler(value = NotLoginException.class)
    public Result<Object> notLoginExceptionHandler(NotLoginException exception, HttpServletRequest request) {
        log.error("NotLoginException {} {}", exception.getMessage(), exception.getClass().getName());
        return Result.fail(ResultCode.UN_AUTHORIZED, exception.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = NotPermissionException.class)
    public Result<Object> notPermissionExceptionHandler(NotPermissionException exception, HttpServletRequest request) {
        log.error("NotPermissionException {} {}", exception.getMessage(), exception.getClass().getName());
        return Result.fail(ResultCode.ACCESS_DENIED, exception.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = NotRoleException.class)
    public Result<Object> notRoleExceptionHandler(NotRoleException exception, HttpServletRequest request) {
        log.error("NotPermissionException {} {}", exception.getMessage(), exception.getClass().getName());
        return Result.fail(ResultCode.ACCESS_DENIED, exception.getMessage());
    }
}
